
export default {
    data(){
        return {
            linkConfig:""
        }
    },
    created(){
        this.linkConfig= this.metadata.options.linkConfig;
    },
    methods:{
        /**
         * 点击链接
         */
        linkClick() {
            let linkConfig = this.linkConfig;
            let url = "";
            //外部链接
            if(!linkConfig){
                uni.showToast({
                    title: '请配置好渲染链接', // 消息内容
                    duration: 2000, // 持续时间（单位：毫秒）
                    icon: 'none' // 图标类型（可选值：success/loading/none）
                });
                return;
            }
            if (linkConfig.type == "external") {
                url=linkConfig.url;
                if(linkConfig.params.length>0){
                    let params = this.getParams(linkConfig.params,false);
                    for (let i = 0; i < linkConfig.params.length; i++) {
                        let alias=linkConfig.params[i].alias;
                        url = url.replace("{"+alias+"}",params[alias]?params[alias]:"");
                    }
                }
                uni.navigateTo({
                    url: '/pages/RxIframe/RxIframe?url=' + url +'&title='+linkConfig.type
                })
                return ;
            }
            if (linkConfig.type == "list") {
                let params = {};
                let key="";
                let name="";
                if(linkConfig.listKey){
                    key = JSON.parse(linkConfig.listKey).value;
                    name = JSON.parse(linkConfig.listKey).label;
                }
                if (linkConfig.params && linkConfig.params.length > 0) {
                    params= this.getParams(linkConfig.params,false);
                }
                this.formVm.openList(key, params);
            } else if (linkConfig.type == "form") {
                let key="";
                let name="";
                if(linkConfig.formKey){
                    key = JSON.parse(linkConfig.formKey).value;
                    name = JSON.parse(linkConfig.formKey).label;
                }
                let params = {};
                if (linkConfig.params && linkConfig.params.length > 0) {
                    let linkParams = this.getParams(linkConfig.params,true);
                    params.params=linkParams;
                }
                params.alias = key;
                //默认为只读
                if (!params.readOnly && params.readOnly != false) {
                    params.readOnly = true;
                }
                this.formVm.openForm(params.alias,name,params.readOnly,params.params.pk, params.params,'');
            } else {
                let params = {};
                if (linkConfig.params && linkConfig.params.length > 0) {
                    let linkParams = this.getParams(linkConfig.params,false);
                    params.params=linkParams;
                }
                url = linkConfig.url;
                this.formVm.openDialog('custom', url, name, params);
            }
        },
        getQueryParams(query){
            let ary=[];
            for(let key in query){
                ary.push(key +"=" + query[key]);
            }
            return ary.join("&")
        },
        getParams(params,isJsonStr) {
            let newParams = {};
            for (let i = 0; i < params.length; i++) {
                let param=params[i];
                let alias=param.alias;
                //需解析json
                if(isJsonStr){
                    if(alias && param.type!="custom"){
                        alias=JSON.parse(alias).value;
                    }
                }
                if (alias && param.field) {
                    //固定值
                    if(param.mappingMode && param.mappingMode== "fixed"){
                        newParams[alias] = param.field;
                    }else {
                        let data=param.table=="curForm"?this.formVm.data:this.data;
                        let v=this.getParamVal(param,data);
                        newParams[alias] = v;
                    }
                }
            }
            return newParams;
        },
        /**
         * 取得参数值。
         * @param param
         * @param data
         * @returns {string|*}
         */
        getParamVal(param,data){
            let val=data [param.field];
            //双值模式。
            if (param.valMode == 'double') {
                if(val){
                    let json = JSON.parse(val);
                    return json.value;
                }else {
                    return "";
                }
            } else {
                //单值
                return val || "";
            }
        }
    }
}
